package com.jzg.jcptadmin.adpter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.MyToast;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.data.local.LocalData;
import com.jzg.jcptadmin.data.vo.CarDetails;
import com.jzg.template.refreshrecycleview.RecycleViewBaseAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by jzg on 2015/12/23.
 */
public class NetworkPicAdapter extends RecycleViewBaseAdapter {
    private BaseActivity context;
    private NetworkPicAdapter adapter;
    private List<CarDetails.CarPicListEntity> list;
    public NetworkPicAdapter(BaseActivity context, List<CarDetails.CarPicListEntity> list) {
        this.context = context;
        adapter = this;
        this.list = list;
    }

    public void update(List<CarDetails.CarPicListEntity> list) {
        this.list = list;
        adapter.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_pic, null));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        if (position == list.size()) {
            ImageLoader.getInstance().displayImage("drawable://" + LocalData.pics[position], myViewHolder.pic);
            myViewHolder.pic_delete.setVisibility(View.GONE);
            if (position == 27) {
                myViewHolder.pic.setVisibility(View.GONE);

            }
        } else {
            if (!TextUtils.isEmpty(list.get(position).getPath())) {
                myViewHolder.pic_delete.setVisibility(View.VISIBLE);
                if (list.get(position).getPath().startsWith("http")) {
                    ImageLoader.getInstance().displayImage(list.get(position).getPath(), myViewHolder.pic, ((AppContext) context.getApplication()).options);
                } else {
                    ImageLoader.getInstance().displayImage("file://" + list.get(position).getPath(), myViewHolder.pic, ((AppContext) context.getApplication()).options);
                }
            } else {
                myViewHolder.pic_delete.setVisibility(View.GONE);
                ImageLoader.getInstance().displayImage("drawable://" + LocalData.pics[position], myViewHolder.pic);
            }

        }
        myViewHolder.textView.setText(LocalData.namesandfujia[position]);
        myViewHolder.pic_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("提示")
                        .setContentText("您确定要删除该照片吗？")
                        .setConfirmText("是的").setCancelText("取消")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sDialog) {
                                try {
                                    int i = holder.getAdapterPosition();
                                    deleteFile(i);
                                    if (i > 17) {
                                        list.remove(i);
                                        adapter.notifyItemRemoved(holder.getAdapterPosition());
                                    } else {
                                        list.get(i).setId(0);
                                        list.get(i).setPath("");
                                        list.get(i).setPathBig("");
                                        adapter.notifyDataSetChanged();
                                    }

                                } catch (Exception e) {
                                    MyToast.showShort(position + "");
                                }

                                sDialog.dismissWithAnimation();
                            }
                        }).show();
            }
        });

    }

    private void deleteFile(int position) {
        try {
            String str = list.get(position).getPath();
            if (str.startsWith("http")) {
            } else {
                File file1 = new File(str);
                File file2 = new File(list.get(position).getPathBig());
                file1.delete();
                file2.delete();
            }
        } catch (Exception e) {
            MyToast.showShort(position + "");
        }


    }


    @Override
    public int getItemCount() {
        if (list.size() == 27) {
            return 27;
        }
        return list.size() + 1;
    }

//    public void update(List<ImageItem> lists) {
//        this.lists = lists;
//        this.notifyDataSetChanged();
//    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView pic;
        ImageView pic_delete;
        TextView textView;

        public MyViewHolder(View view) {
            super(view);
            pic = (ImageView) view.findViewById(R.id.pic_item);
            pic_delete = (ImageView) view.findViewById(R.id.pic_delete);
            textView = (TextView) view.findViewById(R.id.name_text);

        }
    }
}
